﻿package lz.controls.managers
{
    import com.global.GlobalDef;
    
    import flash.display.DisplayObject;
    import flash.display.Stage;
    import flash.events.MouseEvent;
    import flash.utils.Dictionary;

	/**
	 *移动管理 
	 * @author lake
	 * 
	 */	
    public class MoveManager
    {
		/**
		 *旧x记录
		 */		
        private static var _oldX:Dictionary;
		/**
		 *旧y记录 
		 */		
        private static var _oldY:Dictionary;
		/**
		 *对象记录 
		 */		
        private static var _objs:Dictionary;
		/**
		 *是否适应 
		 */		
		private static var isResize :Boolean;

        public function MoveManager()
        {
            
        }
		/**
		 *鼠标移出 
		 * @param event
		 * 
		 */		
        private static function onMouseOut(event:MouseEvent) : void
        {
            var obj:DisplayObject = null;
            for each (obj in _objs)
            {
                
                if (obj)
                {
                    stopMove(obj);
                }
				obj = null;
            }
        }
		/**
		 *开始移动 
		 * @param obj
		 * 
		 */		
        public static function startMove(obj:DisplayObject,
										 isMove:Boolean = true,
										 isSiz:Boolean = false) : void
        {
            if (obj.stage == null)
            {
                return;
            }
			isResize = isSiz;
            init();
            _oldX[obj] = obj.x - obj.stage.mouseX;
            _oldY[obj] = obj.y - obj.stage.mouseY;
            if(isMove)
			{
				obj.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
			}
            obj.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseOut, false, 0, true);
            _objs[obj] = obj;
            limitObj(obj);
		}
		/**
		 *鼠标移动 
		 * @param event
		 * 
		 */		
        private static function onMouseMove(event:MouseEvent) : void
        {
            var obj:DisplayObject = null;
            for each (obj in _objs)
            {
                
                if (obj)
                {
                }
                if (obj.stage)
                {
                    obj.x = _oldX[obj] + obj.stage.mouseX;
                    obj.y = _oldY[obj] + obj.stage.mouseY;
                    limitObj(obj);
                    continue;
                }
                if (obj)
                {
                    stopMove(obj);
                }
            }
        }
		/**
		 *停止移动 
		 * @param obj
		 * 
		 */		
        public static function stopMove(obj:DisplayObject) : void
        {
            if (_oldX)
            {
                delete _oldX[obj];
            }
            if (_oldY)
            {
                delete _oldY[obj];
            }
            if (_objs)
            {
                obj = _objs[obj];
                delete _objs[obj];
                if (obj && obj.stage)
                {
                    obj.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseOut);
                    obj.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
                }
                obj = null;
            }
        }
		/**
		 *限制x，y 
		 * @param obj
		 * 
		 */		
        private static function limitObj(obj:DisplayObject) : void
        {
            if (GlobalDef.GameStage == null)
            {
                return;
            }
            if (obj.x < 0)
            {
                obj.x = 0;
            }
            else if (obj.width + obj.x > GlobalDef.GameStage.stageWidth)
            {
				if(isResize)
				{
					obj.x = GlobalDef.GameStage.mouseX - obj.width - 10;
				}
				else
				{
					obj.x = GlobalDef.GameStage.stageWidth - obj.width;
				}
            }
            if (obj.y < 0)
            {
                obj.y = 0;
            }
            else if (obj.height + obj.y > GlobalDef.GameStage.stageHeight)
            {
				if(isResize)
				{
					obj.y = GlobalDef.GameStage.mouseY - obj.height;
				}
				else
				{
					obj.y = GlobalDef.GameStage.stageHeight - obj.height;
				}
            }
        }
		/**
		 *初始化 
		 * 
		 */		
        private static function init() : void
        {
            if (_oldX == null)
            {
                _oldX = new Dictionary(true);
            }
            if (_oldY == null)
            {
                _oldY = new Dictionary(true);
            }
            if (_objs == null)
            {
                _objs = new Dictionary(true);
            }
        }
    }
}
